package com.hangwei.aicabinet.util;

import android.content.Context;
import android.util.Log;

import com.hangwei.aicabinet.App;
import com.hangwei.aicabinet.MainActivity;
import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.manager.SerialPortProxy;
import com.hangwei.aicabinet.model.entry.ColseDoorFinish;
import com.hangwei.aicabinet.model.entry.ColseDoorUpdateFail;
import com.hangwei.aicabinet.model.entry.ErrorReturnEvent;
import com.hangwei.aicabinet.net.ResponseInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Set;

import rx.Observable;

/**
 * Created by 张龙臣 on 2018/9/18.
 */

public class UpLoadUtil {

    private static Context mContext;

    public static void init(Context context) {
        UpLoadUtil.mContext = context;
    }

    public static void updateLockStatus(final int i) {

//        switch (i) {
//            case 1:
//                if (!LockManager.which1) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which1 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//            case 2:
//                if (!LockManager.which2) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which2 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//            case 3:
//                if (!LockManager.which3) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which3 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//            case 4:
//                if (!LockManager.which4) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which4 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//            case 5:
//                if (!LockManager.which5) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which5 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//            case 6:
//                if (!LockManager.which6) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which6 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//            case 7:
//                if (!LockManager.which7) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which7 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//            case 8:
//                if (!LockManager.which8) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which8 = false;
//                    LockManager.limitTime(i);
//                }
//                break;
//        }
        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                String num = SPUtil.getString(SPKey.OPENED_DOOR_SPACE_NUM);
//                App.tempLock = Integer.parseInt(num);
                return NetManager.getResponseInfoAPI().updateLockStatus(num, i);
            }

            @Override
            protected void onNext(String data) {
//                ToastUtil.showCenterToast("柜门已关闭");
                LogUtil.d("重新开锁");
                SPUtil.setInt(SPKey.OPENED_DOOR, App.tempLock);
                SerialPortProxy.openLock(App.tempLock);
            }

            @Override
            protected void onError() {

            }
        });
    }

    public static void updateLockStatus() {
        Log.d("updateLockStatus--", " isAdmin = " + App.isAdmin);

        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                String num = SPUtil.getString(SPKey.OPENED_DOOR_SPACE_NUM);
                return NetManager.getResponseInfoAPI().updateLockStatus(num, 0);
            }

            @Override
            protected void onNext(String data) {
                ToastUtil.showCenterToast("柜门已关闭");
                LogUtil.d("关锁成功");
                App.tempLock = SPUtil.getInt(SPKey.OPENED_DOOR);
                SPUtil.setInt(SPKey.OPENED_DOOR, 0);
                upLoadTab();
            }

            @Override
            protected void onError() {

            }
        });
    }

    private static void upLoadTab() {
        final String rf = getRFString(SerialPortProxy.getAllTab());
//        LoadingDialog.showDialog(mContext,"")
        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                String num = SPUtil.getString(SPKey.OPENED_DOOR_SPACE_NUM);
                return NetManager.getResponseInfoAPI().doCheckOrders(rf,
                        MainActivity.id, num, SPUtil.getString(SPKey.CABINET_TYPE));
            }

            @Override
            protected void onNext(String data) {
                LogUtil.d("校验成功");
//                if (dialog != null && dialog.isShowing()) dialog.dismiss();
                ToastUtil.showCenterToast("工具检测完毕");
                SPUtil.setString(SPKey.OPENED_DOOR_SPACE_NUM, "");
                EventBus.getDefault().post(new ColseDoorFinish());
            }

            protected void onError(String msg, int code) {
                if (code == 2) {//归还错误
                    Log.e("onError", "归还错误-----------------" + App.tempLock);
                    EventBus.getDefault().post(new ErrorReturnEvent(msg));
                    if (App.tempLock != 0)
                        updateLockStatus(1);
                } else if (code == 5 || code == 3) {//归还错误  单柜
                    Log.e("onError", "归还错误-----------------" + App.tempLock);
                    EventBus.getDefault().post(new ErrorReturnEvent(msg));
                    if (App.tempLock != 0)
                        updateLockStatus(1);
                }
            }

            @Override
            protected void onError() {
                LogUtil.e("标签上传错误");
//                if (dialog != null && dialog.isShowing()) dialog.dismiss();
                //关闭轮询弹窗
                EventBus.getDefault().post(new ColseDoorUpdateFail());
            }
        });
    }

    public static String getRFString(HashMap<String, String> allTab) {
        Set<String> keySet = allTab.keySet();
        if (keySet.size() == 0)
            return "";
        StringBuilder sb = new StringBuilder();
        for (String key : keySet) {
            sb.append(key).append(",");
        }
        String s = sb.toString();
        return s.substring(0, s.length() - 1);
    }
}
